package cn.tedu.oa.sys.controller;

import cn.tedu.oa.common.annotation.LogAnnotation;
import cn.tedu.oa.common.vo.JsonResult;
import cn.tedu.oa.sys.entity.User;
import cn.tedu.oa.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sys/user/")
public class UserController {
    @Autowired
    private UserService userService;

    /*安全设置*/
    @RequestMapping("security_settings")
    public String doUserSecuritySettings() {
        return "sys/user/security_settings";
    }

    /*用户首页*/
    @RequestMapping("user_ui")
    public String doUserUI() {
        return "sys/user/user_ui";
    }

    /*用户首页*/
    @RequestMapping("user_edit")
    public String doUserEditUI() {
        return "sys/user/user_edit";
    }

    /*用户授权角色跳转页面*/
    @RequestMapping("user_accredit")
    public String doUserAccreditUI() {
        return "sys/user/user_accredit";
    }

    /*显示数据列表*/
    @LogAnnotation("用户：分页查询")
    @RequestMapping("get")
    @ResponseBody
    public JsonResult get(String criteria, Integer pageNo, Integer pageSize) {
        return userService.getPagerJsonResult(criteria, pageNo, pageSize);
    }



    /*根据id删除用户*/
    @LogAnnotation("用户：删除")
    @RequiresPermissions("sys:user:delete")
    @RequestMapping("deleteByIds")
    @ResponseBody
    public JsonResult deleteByIds(Integer... ids) {
        userService.deleteByIds(ids);
        return new JsonResult("删除成功");
    }

    /*添加用户*/
    @LogAnnotation("用户：添加")
    @RequiresPermissions("sys:user:add")
    @RequestMapping("addUser")
    @ResponseBody
    public JsonResult addUser(User user) {
        userService.add(user);
        return new JsonResult("添加成功");
    }

    /*修改用户*/
    @LogAnnotation("用户：修改")
    @RequestMapping("updateUser")
    @RequiresPermissions("sys:user:edit")
    @ResponseBody
    public JsonResult updateUser(User user) {
        userService.update(user);
        return new JsonResult("修改成功");
    }

    /*按照id查询用户*/
    @LogAnnotation("用户：按id查询用户")
    @RequestMapping("getUserById")
    @ResponseBody
    public User getUserById(Integer id) {
        return userService.getUserById(id);
    }

    /*获得所有部门id和名称*/
    @LogAnnotation("用户：查询所有部门id和名称")
    @RequestMapping("getDeptIdAndName")
    @ResponseBody
    public JsonResult getDeptIdAndName() {
        return userService.getDeptIdAndName();
    }

    @LogAnnotation("用户：根据用户名获得用户")
    @RequestMapping("getUserByUsername")
    @ResponseBody
    public User getUserByUsername(String username) {
        return userService.getUserByUsername(username);
    }

    @RequiresPermissions("sys:user:accredit")
    @RequestMapping("user_role_add")
    @ResponseBody
    public JsonResult doSaveObject(Integer userId, Integer[] roleId,Integer isdel) {
        userService.insertRoleByUser(userId, roleId,isdel);
        return new JsonResult("授权成功！");
    }



    /*根据id修改密码*/
    @LogAnnotation("用户：修改密码")
    @RequestMapping("updatePasswordById")
    @ResponseBody
    public JsonResult updatePasswordById(String id,String password,String newPassword,String confirmPassword) {
        userService.updatePasswordById(Integer.parseInt(id),password,newPassword,confirmPassword);
        return new JsonResult("修改成功");
    }

    /*基本资料显示页面*/
    @RequestMapping("information")
    public String doInformationUI() {
        return "sys/user/information";
    }
}
